/*
This file is part of asproxy.

asproxy is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

asproxy is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with asproxy.  If not, see <http://www.gnu.org/licenses/>.
*/

#include <stdio.h>
#include "proxyconfigfile.h"

PROXYCONFIG** read_configfile(PROXYCONFIG** proxycollection, int* length)
{
	int nLength = 0;
	char line[1024] = {0};
	FILE* fr = NULL; 
	
	fr = fopen(CONFIGFILE, "rt");
	if (fr == NULL)
	{
		log_error("Could not read contents of asproxy.config. Please make sure it exists and is of the correct format.");
		return proxycollection;
	}
	
	printf("\n***Reading Configuration***\n");
			
	while(fgets(line, 1023, fr) != NULL)
	{
PARSE_CONFIG:		
		// Search for the first valid Proxy Config structure within square brackets
		if (line[0] != '[')
			continue;
		
		// Increment the number of elements
		nLength++;

		// Resize the Proxy Collection
		proxycollection = realloc(proxycollection, nLength * sizeof(PROXYCONFIG *));

		// Create an instance of proxy on the heap
		proxycollection[nLength - 1] = (PROXYCONFIG*)malloc(sizeof(PROXYCONFIG));
		
		// set default values
		memset(proxycollection[nLength - 1], 0, sizeof(PROXYCONFIG));
		strcpy(proxycollection[nLength - 1]->ListeningIP, ANY_DEFAULT);
		strcpy(proxycollection[nLength - 1]->FailoverIP, NONE_DEFAULT);
		proxycollection[nLength - 1]->ConnectionBacklog = CONN_DEFAULT;
		proxycollection[nLength - 1]->BufferSize = BUFFERSIZE_DEFAULT;
		proxycollection[nLength - 1]->SocketTimeout = SOCKTIMEOUT_DEFAULT;
		strcpy(proxycollection[nLength - 1]->SourceDetailFormat, NONE_DEFAULT);
		strcpy(proxycollection[nLength - 1]->SSLCertFile, NONE_DEFAULT);
		strcpy(proxycollection[nLength - 1]->SSLCAPath, NONE_DEFAULT);
		proxycollection[nLength - 1]->WorkerThreadCount = WORKER_DEFAULT;
		strcpy(proxycollection[nLength - 1]->CustomHandler, NONE_DEFAULT);
		proxycollection[nLength - 1]->Debug = DEBUG_DEFAULT;
			
		// Got a valid structure ... Now go through it to find all the elements
		while (fgets(line, 1023, fr) != NULL)
		{
			// Only handle valid properties
			if (line[0] == '#')
				continue;
			else if (line[0] == '[')
				goto PARSE_CONFIG;			
			
			char key[40] = {0};
			char value[40] = {0};
				
			int result = sscanf(line, "%[^'=']=%s\n", key, value);
			if (result != 2)
				continue;

			if (strcmp("listenip", key) == 0) 
				strcpy(proxycollection[nLength - 1]->ListeningIP, value);
			else if (strcmp("destinationip", key) == 0)
				strcpy(proxycollection[nLength - 1]->DestinationIP, value);
			else if (strcmp("failoverip", key) == 0)
				strcpy(proxycollection[nLength - 1]->FailoverIP, value);
			else if (strcmp("listenport", key) == 0)
				proxycollection[nLength - 1]->ListeningPort = atoi(value);
			else if (strcmp("destinationport", key) == 0)
				proxycollection[nLength - 1]->DestinationPort = atoi(value);
			else if (strcmp("connbacklogcount", key) == 0)
				proxycollection[nLength - 1]->ConnectionBacklog = atoi(value);
			else if (strcmp("buffersize", key) == 0)
				proxycollection[nLength - 1]->BufferSize = atoi(value);
			else if (strcmp("sockettimeout", key) == 0)
				proxycollection[nLength - 1]->SocketTimeout = atoi(value);
			else if (strcmp("sourcedetailformat", key) == 0)
				strcpy(proxycollection[nLength - 1]->SourceDetailFormat, value);
			else if (strcmp("sslcertfile", key) == 0)
				strcpy(proxycollection[nLength - 1]->SSLCertFile, value);
			else if (strcmp("sslcapath", key) == 0)
				strcpy(proxycollection[nLength - 1]->SSLCAPath, value);
			else if (strcmp("workerthreadcount", key) == 0)
				proxycollection[nLength - 1]->WorkerThreadCount = atoi(value);
			else if (strcmp("customhandler", key) == 0)
				strcpy(proxycollection[nLength - 1]->CustomHandler, value);
			else if (strcmp("debug", key) == 0)
				proxycollection[nLength - 1]->Debug = value[0];		
		}
	}
	
	fclose(fr);  // close the file prior to exiting the routine    
	
	log_info("Retrieved details from asproxy.config.");
	*length = nLength;
	
	return proxycollection;
}

void free_proxycollection(int length, PROXYCONFIG** proxycollection)
{		
	if (proxycollection != NULL)
	{
		int i = 0;
		for (;i < length;i++)
		{
			if (proxycollection[i] != NULL)
				free(proxycollection[i]);
		}
		
		// Free the array itself
		free(proxycollection);
	}
}

